<?php
// 提交添加宠物信息
// header("content-type:text/html;charset=utf-8");
include_once("conn.php");
include_once("u_getLoginInfo.php");
if($status != 1){
    die("<script>alert('账户处于待注销状态，不可操作！');history.go(-1);</script>");
}

if (isset($_GET['action']) && $_GET['action'] == "del") {
    $petID = $_GET['petID'];
    $select = "SELECT * FROM pet_infos WHERE pet_ID = '" . $petID . "'";
    $res_select = mysqli_query($connect_DB, $select);
    $row_select = mysqli_fetch_object($res_select);

    $str = "../public/pic/pet_photos/" . $userID . "/" . $row_select->pet_PhotoAddress;
    if (file_exists($str)) {
        unlink($str);
    }
    $del_pet = "DELETE FROM pet_infos WHERE pet_ID = '" . $petID . "'";

    // 设置时区为中国
    date_default_timezone_set('Asia/Shanghai');
    // 获取当前时间
    $now_Date = date('Y-m-d');
    $insert_pet_his = "INSERT INTO history_pet_infos (pet_ID,pet_Name,pet_Type,user_ID,pet_DelDate) 
    VALUES ('" . $petID . "','" . $row_select->pet_Name . "','" . $row_select->pet_Type . "','" . $row_select->user_ID . "','" . $now_Date . "')";
    if (mysqli_query($connect_DB, $insert_pet_his)) {
        if (mysqli_query($connect_DB, $del_pet)) {
            echo "<script>alert('删除" . $petID . "成功！');location='user_myPetInfo.php';</script>";
        } else {
            echo $select;
            echo $del_pet;
            echo $insert_pet_his;
            die("<script>alert('删除" . $petID . "失败！');</script>");
        }
    }else{
        echo $insert_pet_his;
        die("<script>alert('插入删除数据库失败！请稍后重试！');history.go(-1);</script>");
    }
} else {
    if(!isset($_POST['petID'])){
        die("<script>alert('非法传参！');history.go(-1);</script>");
    }
    $pet_ID = $_POST['petID'];
    $pet_Name = $_POST['petName'];
    $pet_Type = $_POST['petType'];
    $birthday = $_POST['petBirthday'];
    if ($birthday === '') {
        $birthday = '未填写';
    }
    $photo = $_FILES['petPhoto'];
    if ($photo['name'] != '') {
        if ($pet_Type == 'other') {
            $pet_Type = $_POST['inputPetType'];
        }
        if (!in_array($photo['type'], ['image/jpg', 'image/jpeg', 'image/png'])) {
            die("<script>alert('图片格式错误！请重新选择！');location='user_addPetInfo.php';</script>");
        }
        if ($photo['size'] > 2000000) {
            die("<script>alert('图片大小错误！大小不能超过2M！请重新选择！');location='user_addPetInfo.php';</script>");
        }
        // 图片上传路径
        $uploadAddress = "../public/pic/pet_photos/" . $userID . "/";
        // 图片名称
        $photoName = $pet_ID . "." . pathinfo($photo['name'], PATHINFO_EXTENSION);
        // 完整路径
        $upload = $uploadAddress . $photoName;
        // 检查文件是否已经存在,有则删除
        if (file_exists($upload)) {
            unlink($upload);
        }
        // 创建文件夹（如果不存在）
        if (!file_exists($uploadAddress)) {
            mkdir($uploadAddress, 0777, true);
        }
        if (move_uploaded_file($photo['tmp_name'], $upload)) {
            $update_petinfo = "UPDATE pet_infos SET pet_Name = '" . $pet_Name . "', pet_Type = '" . $pet_Type . "', 
            pet_Birthday = '" . $birthday . "', pet_PhotoAddress = '" . $photoName . "' WHERE pet_ID = '" . $pet_ID . "'";
            if (mysqli_query($connect_DB, $update_petinfo)) {
                echo "<script>alert('更新宠物信息成功！');location='user_myPetInfo.php';</script>";
            } else {
                unlink($upload);
                die("<script>alert('更新宠物信息失败！请重试！');location='user_myPetInfo.php';</script>");
            }
        } else {
            die("<script>alert('图片上传失败！');location='user_myPetInfo.php';</script>");
        }
    }
}

$connect_DB->close();
?>